/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pv168.reminder.swing;

import cz.muni.fi.pv168.reminder.entities.Task;
import cz.muni.fi.pv168.reminder.entities.Worker;
import static cz.muni.fi.pv168.reminder.managers.implementation.TaskManagerImpl.logger;
import cz.muni.fi.pv168.reminder.swing.workers.GetTaskSwingWorker;
import cz.muni.fi.pv168.reminder.swing.workers.GetWorkerSwingWorker;
import cz.muni.fi.pv168.reminder.swing.workers.LoadTasksSwingWorker;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Adic
 */
public class DialogWorkerDetail extends javax.swing.JDialog {

    private Worker worker;
    private Long workerID;

    @Override
    public void setVisible(boolean a) {
        if (worker != null || !a) {
            super.setVisible(a);
        }
    }
    private GetWorkerSwingWorker workerLoadSwingWorker;

    public DialogWorkerDetail(javax.swing.JDialog parent, boolean modal, Long workerID) {
        super(parent, modal);
        this.workerID = workerID;
        initComponents();
        loadData();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDialogError = new javax.swing.JDialog();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();

        jDialogError.setName("jDialogError"); // NOI18N
        jDialogError.setUndecorated(true);

        javax.swing.GroupLayout jDialogErrorLayout = new javax.swing.GroupLayout(jDialogError.getContentPane());
        jDialogError.getContentPane().setLayout(jDialogErrorLayout);
        jDialogErrorLayout.setHorizontalGroup(
            jDialogErrorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jDialogErrorLayout.setVerticalGroup(
            jDialogErrorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("cz/muni/fi/pv168/reminder/swing/Strings"); // NOI18N
        jLabel1.setText(bundle.getString("NAME:")); // NOI18N

        jLabel2.setText(bundle.getString("EMAIL:")); // NOI18N

        jButton1.setText(bundle.getString("SAVE")); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText(bundle.getString("EXIT")); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextField1)
                            .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 90, Short.MAX_VALUE)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(38, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (jTextField1.getText().length() == 0) {
            jLabel1.setText(java.util.ResourceBundle.getBundle("cz/muni/fi/pv168/reminder/swing/Strings").getString("PLEASE INSERT NAME."));
            jDialogError.pack();
            jDialogError.setModal(Boolean.TRUE);
            jDialogError.setVisible(Boolean.TRUE);
            return;
        }
        worker.setName(jTextField1.getText());

        if (jTextField2.getText().length() == 0) {
            jLabel1.setText(java.util.ResourceBundle.getBundle("cz/muni/fi/pv168/reminder/swing/Strings").getString("PLEASE INSERT EMAIL."));
            jDialogError.pack();
            jDialogError.setModal(Boolean.TRUE);
            jDialogError.setVisible(Boolean.TRUE);
            return;
        }
        Pattern pattern = Pattern.compile("[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}");
        Matcher matcher = pattern.matcher(jTextField2.getText().toUpperCase());
        if (!matcher.matches()) {
            jLabel1.setText(java.util.ResourceBundle.getBundle("cz/muni/fi/pv168/reminder/swing/Strings").getString("PLEASE INSERT CORRECT EMAIL"));
            jDialogError.pack();
            jDialogError.setModal(Boolean.TRUE);
            jDialogError.setVisible(Boolean.TRUE);
            return;
        }
        worker.setEmailAddress(jTextField2.getText());
        setVisible(false);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        setVisible(Boolean.FALSE);
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(DialogWorkerDetail.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(DialogWorkerDetail.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(DialogWorkerDetail.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(DialogWorkerDetail.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {

            }
        });

    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JDialog jDialogError;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables

    public Worker getWorker(){
        return worker;
    }
    /*public List<Worker> getWorkers(){
        return ((WorkerListModel) jListWorkers.getModel()).getWorkers();
    }*/
    private void loadData() {
        worker = null;

        workerLoadSwingWorker = new GetWorkerSwingWorker(workerID);
        workerLoadSwingWorker.execute();
        try {
            worker = workerLoadSwingWorker.get();
        } catch (InterruptedException | ExecutionException ex) {
            Logger.getLogger("Reminder").log(Level.SEVERE, "Can't load worker with id: " + workerID, ex);
        }

        if (worker == null) {
            jLabel1.setText(java.util.ResourceBundle.getBundle("cz/muni/fi/pv168/reminder/swing/Strings").getString("SORRY, CAN'T LOAD SELECTED WORKER."));
            jDialogError.pack();
            jDialogError.setVisible(Boolean.TRUE);
            this.worker = null;
            setVisible(Boolean.FALSE);

            return;
        }

        jTextField1.setText(worker.getName());
        jTextField2.setText(worker.getEmailAddress());

        LoadTasksSwingWorker asd = new LoadTasksSwingWorker(false, false, true, false);
        asd.execute();
        List<Task> tasks=new ArrayList<>();
        try {
            tasks = asd.get();
            logger.info("Tasks loaded successfully");
        } catch (InterruptedException | ExecutionException ex) {
            logger.log(Level.SEVERE, "Can't load tasks.", ex);
            DBError error = new DBError(null, true);
            error.pack();
            error.setVisible(true);
        }
        if (tasks == null) {
            tasks = new ArrayList<>();
        }
        
        for(Task task : tasks) {
          
        }
    

}
    
    

}
